package com.elock.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.controller.BaseController;
import com.elock.base.core.controller.ResultMsg;
import com.elock.base.core.util.DateUtil;
import com.elock.base.core.util.JsonUtils;
import com.elock.entity.PcMenu;
import com.elock.entity.PcRole;
import com.elock.service.PcMenuBaseService;
import com.elock.service.PcRoleBaseService;
import com.elock.service.PcUserRoleBaseService;
import com.elock.service.SaleOrgBaseService;

/**
 * Controller - 菜单角色
 * @author 仁德
 */
@Controller("adminMenuRoleController")
@RequestMapping("/admin_menu_role")
public class AdminMenuRoleController extends BaseController {

	@Resource(name = "pcUserRoleBaseServiceImpl")
	private PcUserRoleBaseService pcUserRoleBaseService;
	@Resource(name = "pcRoleBaseServiceImpl")
	private PcRoleBaseService pcRoleBaseService;
	@Resource(name = "pcMenuBaseServiceImpl")
	private PcMenuBaseService pcMenuBaseService;
	@Resource(name = "saleOrgBaseServiceImpl")
	private SaleOrgBaseService saleOrgBaseService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list_tb", method = RequestMethod.GET)
	public String list_tb(ModelMap model) {
		return "/admin_menu_role/list_tb";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Pageable pageable, ModelMap model) {

		return "/admin_menu_role/list";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/list_data", method = RequestMethod.POST)
	public @ResponseBody ResultMsg list_data(String name, Boolean isSystem,
			String firstTime, String lastTime, Pageable pageable) {

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.isNull("companyInfoId"));
		if (StringUtils.isNotEmpty(name)) {
			filters.add(Filter.like("name", "%" + name + "%"));
		}
		if (isSystem != null) {
			filters.add(Filter.eq("isSystem", isSystem));
		}
		if (StringUtils.isNotEmpty(firstTime)) {
			filters.add(
					Filter.ge("createDate", DateUtil.convert(firstTime + " 00:00:00")));
		}
		if (StringUtils.isNotEmpty(lastTime)) {
			filters.add(Filter.lt("createDate",
					DateUtils.addDays(DateUtil.convert(lastTime + " 00:00:00"), 1)));
		}
		Page<PcRole> page = pcRoleBaseService.findPage(filters, null, pageable);

		String jsonPage = JsonUtils.toJson(page);

		return success(jsonPage);
	}

	/**
	 * 添加
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model) {

		List<PcMenu> menus = pcMenuBaseService.getCurrentMenuList(1);
		model.addAttribute("menus", menus);

		return "/admin_menu_role/add";
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody ResultMsg save(PcRole pcRole, Long[] menuId) {

		if (pcRole.getName() == null) {
			return error("请填写角色名称");
		}
		List<Filter> fis = new ArrayList<Filter>();
		fis.add(Filter.eq("name", pcRole.getName()));
		List<PcRole> pcRoles = pcRoleBaseService.findList(null, fis, null);
		if (pcRoles != null && pcRoles.size() > 0) {
			return error("该角色名称已存在，请重新维护");
		}

		List<PcMenu> menus = new ArrayList<PcMenu>();
		for (Long mId : menuId) {
			PcMenu menu = pcMenuBaseService.find(mId);
			menus.add(menu);
		}
		pcRole.setPcMenus(new HashSet<PcMenu>(menus));
		pcRole.setIsSystem(false);
		pcRoleBaseService.save(pcRole);
		return success().addObjX(pcRole.getId());
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {

		model.addAttribute("pcRole", pcRoleBaseService.find(id));
		List<PcMenu> menus = pcMenuBaseService.getCurrentMenuList(1);
		model.addAttribute("menus", menus);
		return "/admin_menu_role/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody ResultMsg update(PcRole pcRole, Long[] menuId) {

		PcRole pPcRole = pcRoleBaseService.find(pcRole.getId());
		if (pPcRole == null) {
			return error("16350");
		}

		if (pcRole.getName() == null) {
			return error("请填写角色名称");
		}
		List<Filter> fis = new ArrayList<Filter>();
		fis.add(Filter.ne("id", pcRole.getId()));
		fis.add(Filter.eq("name", pcRole.getName()));
		List<PcRole> pcRoles = pcRoleBaseService.findList(null, fis, null);
		if (pcRoles != null && pcRoles.size() > 0) {
			return error("该角色名称已存在，请重新维护");
		}

		List<PcMenu> menus = new ArrayList<PcMenu>();
		for (Long mId : menuId) {
			PcMenu menu = pcMenuBaseService.find(mId);
			menus.add(menu);
		}
		pcRole.getPcMenus().clear();
		pcRole.setPcMenus(new HashSet<PcMenu>(menus));
		if (pPcRole.getIsSystem()) {
			pcRoleBaseService.update(pcRole, "name", "isSystem", "companyInfoId");
		}
		else {
			pcRoleBaseService.update(pcRole, "isSystem", "companyInfoId");
		}
		return success();
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody ResultMsg delete(Long[] ids) {

		if (ids != null && ids.length > 0) {
			for (Long id : ids) {
				PcRole pcRole = pcRoleBaseService.find(id);
				if (pcRole == null) {
					return error("16350");
				}
				if (pcRole.getIsSystem()) {
					return error("16351");
				}
				if (pcUserRoleBaseService.count(Filter.eq("pcRole", pcRole)) > 0) {
					return error("16352", pcRole.getName());
				}
			}
			pcRoleBaseService.delete(ids);
		}
		else {
			return error("16353");
		}
		return success();

	}

}
